package com.itheima.service;

import com.alibaba.dubbo.config.annotation.Service;
import com.itheima.pojo.CheckGroup;
import com.itheima.pojo.CheckItem;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ObjectUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.dao.CheckGroupDao;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
@Service(interfaceClass = CheckGroupService.class)
@Transactional//应该加在方法上 先加在这里
public class CheckGroupServiceImpl implements CheckGroupService {

    @Autowired
    private CheckGroupDao checkGroupDao;


    @Override
    public PageResult find(QueryPageBean queryPageBean) {
        Page<CheckGroup> page = PageHelper.startPage(queryPageBean.getCurrentPage(), queryPageBean.getPageSize())
                .doSelectPage(() -> checkGroupDao.find(queryPageBean.getQueryString()));
        PageResult pageResult =new PageResult(page.getTotal(),page.getResult());
        return pageResult;

    }

    @Override
    public ArrayList<Integer> findIdsBygroupId(Integer id) {
        return (ArrayList<Integer>) checkGroupDao.findIdsBygroupId(id);
    }

    @Override
    public CheckGroup findById(Integer id) {
        //需要查询中间表 获取id的几个 然后遍历 调用查询检查项的方法 获取出来的对象给Checkgroup赋值
        CheckGroup checkGroup = checkGroupDao.findById(id);
        ArrayList<CheckItem> checkItems = (ArrayList<CheckItem>) checkGroupDao.findGroupById(id);
        if (checkItems.size()>=0){
            checkGroup.setCheckItems(checkItems);
        }
        return checkGroup;
    }

    @Override
    public Boolean update(ArrayList<Integer> checkitemIds,CheckGroup checkGroup) {
        //执行更新操作
        if (!ObjectUtils.isEmpty(checkGroup)){
            //更新基本信息
            Boolean res = checkGroupDao.update(checkGroup);
            List<Integer> list = checkGroupDao.findIdsBygroupId(checkGroup.getId());
            for (int i = 0; i < list.size(); i++) {
                for (int j = 0; j < checkitemIds.size(); j++) {
                    if(list.get(i).equals(checkitemIds.get(j))){
                        list.remove(i);
                        checkitemIds.remove(j);
                    }
                }
            }
            //经过这一步操作  checkitemIds里面是中间表需要增加的  list里面是中间表需要删除的
            //接下来多中检查项 检查组 中间表进行操作
            //1.先删除
            for (Integer integer : list) {
                checkGroupDao.deletebyId(checkGroup.getId(), integer);
            }
            //2.再添加
            for (Integer checkitemId : checkitemIds) {
                checkGroupDao.InsertbyId(checkGroup.getId(),checkitemId);
            }
            return true;
            //更新完基本信息 就要进行
        }else {
            return false;
        }
    }
    /**
     * 检查组删除
     *
     * @param id
     * @return
     */
    @Override
    @Transactional
    public Integer delete(Integer id) {
        Integer deleteGroupSetId = checkGroupDao.findSetId(id);

        if (deleteGroupSetId > 0) {
            return -1;
        }
        this.checkGroupDao.deleteGroupAndItem(id);
        Integer delete = checkGroupDao.delete(id);
        return delete;
    }
    

    @Override
    @Transactional
    public Integer add(CheckGroup checkGroup, List<Integer> ids) {
        try {
            Integer count = this.checkGroupDao.addCheckGroup(checkGroup);
            Integer gid = checkGroup.getId();
            //不要用for循环？xml foreach批量添加 10000 -> 500~1000
            for (Integer id : ids) {
                Map<String, Integer> map = new HashMap<>();
                map.put("checkgroup_id", gid);
                map.put("checkitem_id", id);
                this.checkGroupDao.addGroupAndItem(map);
            }
            return 1;
        } catch (Exception e) {
            e.printStackTrace();
            return -1;
        }
    }
}
